package com.linq.cool.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linq.cool.common.core.exception.BusinessException;
import com.linq.cool.common.core.utils.SecurityUtils;
import com.linq.cool.system.domain.SysPost;
import com.linq.cool.system.domain.SysUserPost;
import com.linq.cool.system.dto.SysPostQueryDTO;
import com.linq.cool.system.mapper.SysPostMapper;
import com.linq.cool.system.service.SysPostService;
import com.linq.cool.system.service.SysUserPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2020/12/29 19:44
 * @Description:
 * @Version: 1.0.0
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {
    @Autowired
    private SysUserPostService sysUserPostService;

    @Override
    public IPage<SysPost> listByPage(IPage<SysPost> page, SysPostQueryDTO dto) {
        return this.page(page, new LambdaQueryWrapper<SysPost>()
                .like(StringUtils.checkValNotNull(dto.getCode()), SysPost::getCode, dto.getCode())
                .eq(StringUtils.checkValNotNull(dto.getStatus()), SysPost::getStatus, dto.getStatus())
                .like(StringUtils.checkValNotNull(dto.getName()), SysPost::getName, dto.getName())

        );
    }

    @Override
    public boolean checkNameUnique(SysPost post) {
        // 判断传入的是否是空(新增或者更细) 新增的时候id roleId=-1  更新就是原来的
        Long roleId = null == post.getId() ? -1L : post.getId();
        SysPost one = baseMapper.selectOne(new LambdaQueryWrapper<SysPost>()
                                                   .eq(SysPost::getName, post.getName())
        );
        // 判断id是否一致 不一致就代表 唯一 false
        return null != one && !roleId.equals(one.getId());
    }

    @Override
    public boolean checkCodeUnique(SysPost post) {
        // 判断传入的是否是空(新增或者更细) 新增的时候id roleId=-1  更新就是原来的
        Long roleId = null == post.getId() ? -1L : post.getId();
        SysPost one = baseMapper.selectOne(new LambdaQueryWrapper<SysPost>()
                                                   .eq(SysPost::getCode, post.getCode())
        );
        // 判断id是否一致 不一致就代表 唯一 false
        return null != one && !roleId.equals(one.getId());
    }

    @Override
    public boolean deleteByIds(List<Long> postIds) {
        postIds.forEach(id -> {
            SysPost post = getById(id);
            int count = sysUserPostService.count(new LambdaQueryWrapper<SysUserPost>()
                                                         .eq(SysUserPost::getPostId, id)
            );
            if (count > 0) {
                throw new BusinessException(String.format("%1$s已分配,不能删除", post.getName()));
            }
        });
        return this.removeByIds(postIds);
    }

    @Override
    public List<Long> listByUserId(Long userId) {
        return baseMapper.listByUserId(userId);
    }

    @Override
    public List<SysPost> listByParams(SysPost post) {
        return this.list(this.getLambdaQueryWrapper(post));
    }

    @Override
    public boolean savePost(SysPost post) {
        if (this.checkNameUnique(post)) {
            throw new BusinessException("新增岗位'" + post.getName() + "'失败，岗位名称已存在");
        }
        if (this.checkCodeUnique(post)) {
            throw new BusinessException("新增岗位'" + post.getName() + "'失败，岗位编码已存在");
        }
        post.setCreator(SecurityUtils.getUserId());
        return this.save(post);
    }

    @Override
    public boolean updatePost(SysPost post) {
        if (this.checkNameUnique(post)) {
            throw new BusinessException("修改岗位'" + post.getName() + "'失败，岗位名称已存在");
        }
        if (this.checkCodeUnique(post)) {
            throw new BusinessException("修改岗位'" + post.getName() + "'失败，岗位编码已存在");
        }
        post.setModifier(SecurityUtils.getUserId()).setGmtModified(new Date());
        return this.updateById(post);
    }

    @Override
    public List<SysPost> listPostsByUserId(Long userId) {
        return baseMapper.listPostsByUserId(userId);
    }

    private LambdaQueryWrapper<SysPost> getLambdaQueryWrapper(SysPost post) {
        return new LambdaQueryWrapper<SysPost>()
                .like(StringUtils.checkValNotNull(post.getCode()), SysPost::getCode, post.getCode())
                .eq(StringUtils.checkValNotNull(post.getStatus()), SysPost::getStatus, post.getStatus())
                .like(StringUtils.checkValNotNull(post.getName()), SysPost::getName, post.getName());

    }
}
